package com.example.seakdreamandroid.sleepRecord;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.example.seakdreamandroid.R;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;
public class SleepRecordActivity extends FragmentActivity {
    private ImageView imgReturn;
    private TabLayout tab;
    private ViewPager2 vp2;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private SleepRecordFragmentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sleep_record);
        initFragments();
        getView();
        setListener();
        adapter = new SleepRecordFragmentAdapter(this,fragments);

        vp2.setAdapter(adapter);
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        int id = getIntent().getIntExtra("page", 0);

        if (id == 2) {
            vp2.setCurrentItem(2);
        }
        TabLayoutMediator mediator = new TabLayoutMediator(tab,vp2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch(position){
                    case 0:
                        tab.setText("日");
                        break;
                    case 1:
                        tab.setText("周");
                        break;
                    case 2:
                        tab.setText("月");
                        break;
                }
            }
        });
        mediator.attach();
    }

    private void initFragments(){
        fragments.add(new RecordPerDay());
        fragments.add(new RecordPerWeek());
        fragments.add(new RecordPerMonth());
    }

    private void setListener() {
        imgReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SleepRecordActivity.this.finish();
            }
        });
    }

    private void getView() {
        imgReturn = findViewById(R.id.img_return);
        tab = findViewById(R.id.tab);
        vp2 = findViewById(R.id.vp2);
    }
}